//## 제품 정보 저장 ####################
function fnSave()
{
	if (!$("#prdNm").val())
	{
		alert("제품명을 바르게 입력해 주세요!");
		$("#prdNm").focus();
		return;
	}
	var valUrl  = '/action/product/save.asp';
	var param  = "prdNm="+encodeURIComponent($("#prdNm").val());
		param += "&etcPrdCd="+encodeURIComponent($("#etcPrdCd").val());
		param += "&remarks="+encodeURIComponent($("#remarks").val());
		param += "&sourceSize="+encodeURIComponent($("#sourceSize").val());
		param += "&compCd="+$("#compCd").val();
		param += "&flag="+$("#flag").val();
		param += "&prdGrp="+$("#prdGrp").val();
		param += "&prdCd="+$("#prdCd").val();
		param += "&unitPrice="+$("#unitPrice").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 거래 정보 그리드 표시 ####################
var G_curPage01 = 1;

function grid01List(page)
{	
	var gridNo = 1;
	var xmlNode = "PrdList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/product/list.asp";
	var param = "findPrdNm="+encodeURIComponent($("#findPrdNm").val());
		param += "&findEtcPrdCd="+encodeURIComponent($("#findEtcPrdCd").val());
		param += "&findMkType="+$("#findMkType").val();
		param += "&findGoods="+$("#findGoods").val();
		param += "&findPrdGrp="+$("#findPrdGrp").val();
		
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+param);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,left,left,,,,,".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlGood = 0;
			var ttlBad = 0;
			var ttlQty = 0;
			$(xml).find(xmlNode).each(function(idx){

				if (k>startNo && k<=endNo)
				{
					var prdCd			= $(this).find('prdCd').text();
					var prdNm			= $(this).find('prdNm').text();
					var etcPrdCd		= $(this).find('etcPrdCd').text();
					var company		= $(this).find('company').text();
					var part				= $(this).find('part').text();
					var remarks			= $(this).find('remarks').text();
					var prdGrp			= $(this).find('prdGrp').text();
					var flag				= $(this).find('flag').text();
					var totalGoods	= $(this).find('totalGoods').text();
					
					if(flag == "Y") showFlag = "생산";
					else if(flag == "H") showFlag = "중지";
					else showFlag = "삭제";
					
					var arrVal = new Array();
					arrVal[0] = "<p>"+no+"</p>";
					arrVal[1] = "<p>"+etcPrdCd+"</p>";
					arrVal[2] = "<p>"+prdNm+"</p>";
					arrVal[3] = "<p>"+company+"</p>";
					arrVal[4] = "<p>"+totalGoods+"</p>";
					arrVal[5] = "<p>"+prdGrp+"</p>";
					arrVal[6] = "<p>"+showFlag+"</p>";
					
					if (flag == "N") bgColor = "#EEE";
					else bgColor = "#FFF";
					
					var link = "fnSetProduct('"+prdCd+"')";

					
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 거래처 상세정보 표시 ####################
function fnSetProduct(prdCd)
{
	var valUrl = "/action/product/view.asp";
	var param = "prdCd="+prdCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			
			$("#prdCd").val($(xml).find("prdCd").text());
			$("#prdNm").val($(xml).find("prdNm").text());
			$("#prdGrp").val($(xml).find("prdGrp").text());
			$("#etcPrdCd").val($(xml).find("etcPrdCd").text());
			//$("#part").val($(xml).find("part").text());
			$("#remarks").val($(xml).find("remarks").text());
			$("#sourceSize").val($(xml).find("sourceSize").text());
			$("#unitPrice").val($(xml).find("unitPrice").text());
			$("#flag").val($(xml).find("flag").text());

			fnFindCompany('',$(xml).find("compCd").text(),'compCd');			
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 작업지시 내역 띄우기
function fnOrderDetail()
{
	if (!$("#prdNm").val())
	{
		alert('제품을 선택해 주세요');
		return;
	}
	window.open("jobOrder.asp?prdNm="+$("#prdNm").val());
}

//## 제품 정보 Reset 기능
function fnReset()
{

	$("#prdCd").val("");
	$("#prdNm").val("");
	$("#etcPrdCd").val("");
	$("#compNm").val("");
	$("#sourceSize").val("");
	$("#compCd").val("");
	$("#prdGrp").val("");
	//$("#part").val("");
	$("#remarks").val("");
	$("#unitPrice").val("");
	$("#flag").val("Y");
	$("#accessInfo").val("");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findPrdNm").val("");
	$("#findEtcPrdCd").val("");
	$("#findType").val("");
	$("#findPrdGrp").val("");
	$("#findGoods").val("0");
}